#include "Scheduler.h"

#include "show.h"
#include "Stick.h"
#include "Param.h"
#include "ANODT.h"
#include "KoDoKey.h"
#include "KoDoLED.h"
#include "KoDoNRF24l01.h"

uint32_t SysTick_count = 0;

/*消息队列控制块*/
rt_mq_t RC_mq;

static rt_thread_t  Radio_Thread;
static rt_thread_t  Rocker_Thread;
static rt_thread_t  Key_Thread;
static rt_thread_t  Display_Thread;
static rt_thread_t  Battery_Thread;

/*无线连接任务*/
void RadioTask(void *parameter)   //2ms执行一次
{
  while(1)
  {
      NRF_Check_Event(); //检查是否收到飞机数据
      if(NRF_Evnet)      //如果收到数据就把它发到上位机
      {
          NRF_Evnet = 0;
      }
      Stick_Scan();  //读取遥杆等数据
      rt_thread_mdelay(2);
  }
}

/*数据发送*/
void RockerTask(void *parameter)    //2ms执行一次
{
  rt_err_t RC_Receive = RT_EOK;
  struct _Rc p;
  while(1)
  {
      //获取消息队列数据
      RC_Receive = rt_mq_recv(RC_mq,&p,sizeof(p),0);
      if(RC_Receive == RT_EOK)
      {
          Rc.THR = p.THR; Rc.PIT = p.PIT;
          Rc.YAW = p.YAW; Rc.ROL = p.ROL;
          Rc.AUX1 = p.AUX1;
          Rc.AUX3 = p.AUX3;
          Rc.AUX5 = p.AUX5;
      }
      Send_RCData();     //发送控制数据给飞机
      rt_thread_mdelay(5);
  }
}

/*按键扫描任务*/
void KeyTask(void *parameter)   //20ms执行一次
{
  while(1)
  {
    key_function();         //按键检测
    Gesture_Check();        //显示屏摇杆上下选择操作检测
    SysTick_count++;        //按键扫描延时用
    ANO_key();              //按键扫描
    NRF_Check_Ch();         //自动对频检测
    rt_thread_mdelay(20);
  }
}

/*显示任务*/
void DisplayTask(void *parameter) //30ms执行一次
{
  while(1)
  {
    Show_Duty();    //显示程序
    if(send_flag)   //发送设置数据到飞机
       Send_Flag_To_Fly(set_temp,0);
    rt_thread_mdelay(40);
  }
}

void BatteryTask(void *parameter) // 1000ms执行一次
{
  while(1)
  {
    //更新电压值
    Show.Battery_Rc = Rc.AUX5;
    if(Show.Battery_Rc<345 && Show.Battery_Rc>320)  //判断电压提示电池电量低
        Show.low_power = 1;
    else
        Show.low_power = 0;

    //计算收到飞机数据的帧率
    NRF_SSI = NRF_SSI_CNT;
    Rc.AUX6 = NRF_SSI;
    NRF_SSI_CNT = 0;

    /*如果帧率为0标记为失联状态*/
    if(NRF_SSI==0)
    {
        LED_blue_OFF;
        Show.Rc_num = 0;
        Show.Connect_Succeed = 0;
        Show.hardware_type = 0;
        Show.test_flag = 0;
        Show.windows = 0;
    }
    else
    {
        LED_blue_ON;
        Show.Connect_Succeed = 1;
    }
    rt_thread_mdelay(1000);
  }
}

void Task_Init(void)
{
    //创建消息队列
    RC_mq = rt_mq_create("RC_mq",32,5,RT_IPC_FLAG_FIFO);
    //if(RC_mq != RT_NULL) rt_kprintf("RC_mq消息队列创建成功\r\n");

    Radio_Thread = rt_thread_create("Radio",RadioTask,RT_NULL,220,2,1);
    if(Radio_Thread != RT_NULL) rt_thread_startup(Radio_Thread);
    Rocker_Thread = rt_thread_create("Rocker",RockerTask,RT_NULL,236,3,1);
    if(Rocker_Thread != RT_NULL) rt_thread_startup(Rocker_Thread);
    Key_Thread = rt_thread_create("Key",KeyTask,RT_NULL,200,4,1);
    if(Key_Thread != RT_NULL) rt_thread_startup(Key_Thread);
    Battery_Thread = rt_thread_create("Battery",BatteryTask,RT_NULL,220,5,1);
    if(Battery_Thread != RT_NULL) rt_thread_startup(Battery_Thread);
    Display_Thread = rt_thread_create("Display",DisplayTask,RT_NULL,320,5,1);
    if(Display_Thread != RT_NULL) rt_thread_startup(Display_Thread);
}
